﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.IO;
using System.Collections;


namespace NewPagePatroller
{
    public partial class MainWindow : Form
    {
        CentralStuff central;
        string username, password, project;

        public MainWindow()
        {
            central = new CentralStuff();
            central.RecentChanges = new Queue();

            SplashScreen splash = new SplashScreen();
            splash.ShowDialog();

            central.cookieJar = new CookieContainer();
            InitializeComponent();

            
            username = password = project = "";
            
            LoginWindow login = new LoginWindow(ref username, ref password, ref project);
            string apiParameters = "format=xmlfm&action=login&lgname=" + username + "&lgpassword=" + password;
            webBrowser1.DocumentStream = central.sendWebRequest(central._API_, apiParameters, NewPagePatroller.CentralStuff.HttpMethod.HTTP_POST).GetResponseStream();

           
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {

            string apiParameters = "format=xmlfm&action=login&lgname=" + username + "&lgpassword=" +password;
            webBrowser1.DocumentStream= central.sendWebRequest(central._API_, apiParameters, NewPagePatroller.CentralStuff.HttpMethod.HTTP_POST).GetResponseStream();

            updateNewPagesList();
        }

        private void updateNewPagesList()
        {
            string apiParameters = "format=xml&action=query&list=recentchanges&rctype=new&rcnamespace=0&rcshow=!patrolled&rcprop=user|comment|timestamp|title|ids&rclimit=100&rcdir=older&rctoken=patrol";

            central.RecentChanges = new System.Collections.Queue();

            HttpWebResponse rsvp = central.sendWebRequest(central._API_, apiParameters, NewPagePatroller.CentralStuff.HttpMethod.HTTP_GET);
            if (rsvp == null) return;
            XmlTextReader updateListReader = new XmlTextReader(rsvp.GetResponseStream());
            updateListReader.Read();
            while (updateListReader.Name != "query")
            {
                updateListReader.Read();
            }

            updateListReader.Read(); // recentchanges
            updateListReader.Read(); // first rc

            while (updateListReader.Name == "rc")
            {
                RecentChange rc = new RecentChange();
                rc.ns = updateListReader.GetAttribute("ns");
                rc.title = updateListReader.GetAttribute("title");
                rc.rcid = updateListReader.GetAttribute("rcid");
                rc.comment = updateListReader.GetAttribute("comment");
                rc.timestamp = updateListReader.GetAttribute("timestamp");
                rc.user = updateListReader.GetAttribute("user");
                rc.patroltoken = updateListReader.GetAttribute("patroltoken");

                central.RecentChanges.Enqueue(rc);

                updateListReader.Read();
            }

            updateQueueCount();

        }

        private void updateQueueCount()
        {
            string s = "";
            if (central.RecentChanges.Count != 1)
                s = "s";

            label1.Text = string.Format("{0} item{1} in queue", central.RecentChanges.Count, s);
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://en.wikipedia.org/wiki/Special:UserLogout");
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public void getNextRecentChange()
        {
            central.currentChange = (RecentChange)central.RecentChanges.Dequeue();

            toolStripLabel3.Text = central.currentChange.timestamp;
            toolStripLabel2.Text = central.currentChange.user;
            this.Text = central.currentChange.title + " - New Page Patroller";

            HttpWebResponse rsvp = central.sendWebRequest(
                central._API_, 
                "page=" + central.currentChange.title + "&action=parse&format=xml",
                NewPagePatroller.CentralStuff.HttpMethod.HTTP_GET
                );
            if(rsvp == null) return;

            XmlTextReader apiReader = new XmlTextReader(rsvp.GetResponseStream());

            apiReader.Read();
            while (apiReader.Name != "text")
                apiReader.Read();





            apiReader.Read();
            webBrowser1.DocumentText = getStyleHeaders() + apiReader.ReadContentAsString();

            toolStripLabel5.Text = central.currentChange.comment;

            updateQueueCount();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            getNextRecentChange();
        }

       

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

            HttpWebResponse rsvp = central.sendWebRequest(central._API_, "action=patrol&token=" +
                central.encodeToken(central.currentChange.patroltoken) + "&rcid=" + central.currentChange.rcid,
                NewPagePatroller.CentralStuff.HttpMethod.HTTP_GET);
            getNextRecentChange();
        }

        



        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }

        private void showCurrentUserDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.DocumentStream= central.sendWebRequest(central._API_, "action=query&meta=userinfo&uiprop=hasmsg|blockinfo|groups|rights|options|editcount|ratelimits", NewPagePatroller.CentralStuff.HttpMethod.HTTP_GET).GetResponseStream();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            central.RecentChanges.Clear();
            updateNewPagesList();
        }

        private string getStyleHeaders()
        {
            string styleHeaders = "<html><head><style type=\"text/css\">";
            styleHeaders += getStyleSheet("http://en.wikipedia.org/skins-1.5/common/shared.css","");
            styleHeaders += getStyleSheet("http://en.wikipedia.org/skins-1.5/monobook/main.css","");
            styleHeaders += getStyleSheet("http://en.wikipedia.org/skins-1.5/monobook/IE70Fixes.css","");
            styleHeaders += getStyleSheet("http://en.wikipedia.org/w/index.php","title=MediaWiki:Common.css&usemsgcache=yes&ctype=text/css&smaxage=2678400&action=raw&maxage=2678400");
            styleHeaders += getStyleSheet("http://en.wikipedia.org/w/index.php","title=MediaWiki:Monobook.css&usemsgcache=yes&ctype=text/css&smaxage=2678400&action=raw&maxage=2678400");
            styleHeaders += getStyleSheet("http://en.wikipedia.org/w/index.php","title=User:Stwalkersock/npp.css&action=raw&ctype=text/css");

            styleHeaders += "</style></head><body>";
            return styleHeaders ;
        }

        private string getStyleSheet(string uri, string parameters)
        {
            HttpWebResponse rsvp = central.sendWebRequest(uri, parameters, NewPagePatroller.CentralStuff.HttpMethod.HTTP_GET);
            if (rsvp == null) return "";

            StreamReader sr = new StreamReader(rsvp.GetResponseStream());
            return sr.ReadToEnd();
        }
    }
}
